<?php
namespace api\task\controller;


use api\common\model\RlHiseServiceProjectModel;
use api\common\model\RlZsmsUserQueryModel;
use think\Controller;
use think\Db;
use think\Log;
use api\common\service\MsgService;
use api\common\model\RlApplyDelayModel;
ini_set('max_execution_time', 6000);
class AutomaticController extends Controller
{
    public function Automaticlog(){
        $count=RlHiseServiceProjectModel::where('gczt', 'in', '3,4')->where('ycgcsfjsbs', 1)->count();
        $page=ceil($count/200);
        for($i=0;$i<=$page;$i++){
            $azd_list = RlHiseServiceProjectModel::where('gczt', 'in', '3,4')->where('ycgcsfjsbs', 1)->limit($i*200,200)->select()->toArray();
            foreach ($azd_list as $v) {
                $yqsqzq = RlApplyDelayModel::where(['status' => 1, 'azd_id' => $v['azd_id']])->sum('yqsqzq');
                $begintime = strtotime(date('Y-m-d 00:00:00', time()));
                $endtime = strtotime(date('Y-m-d 23:59:59', time()));
                $time = 8 + $yqsqzq;
                $yqtime = strtotime('+' . $time . ' month', $v['ycgcwcsj']);
                if ($yqtime >= $begintime && $yqtime <= $endtime) {
                    $azd_arr['azd_id'] = $v['azd_id'];
                    $azd_arr['create_time'] = date("Y-m-d H:i:s", time());
                    $azd_arr['status'] = 0;
                    $azd_arr['type'] = 1;
                    Db::name('message_log')->insert($azd_arr);
                }
            }
        }
    }
    public function Automatic(){
        $count=RlHiseServiceProjectModel::where('gczt', 'in', '3,4')->where('ycgcsfjsbs', 1)->count();
        $page=ceil($count/200);
        for($i=0;$i<=$page;$i++){
            $azd_list = RlHiseServiceProjectModel::where('gczt', 'in', '3,4')->where('ycgcsfjsbs', 1)->limit($i*200,200)->select()->toArray();
            foreach ($azd_list as $v) {
                $yqsqzq = RlApplyDelayModel::where(['status' => 1, 'azd_id' => $v['azd_id']])->sum('yqsqzq');
                $begintime = strtotime(date('Y-m-d 00:00:00', time()));
                $endtime = strtotime(date('Y-m-d 23:59:59', time()));
                $time = 8 + $yqsqzq;
                $yqtime = strtotime('+' . $time . ' month', $v['ycgcwcsj']);
                if ($yqtime >= $begintime && $yqtime <= $endtime) {
                    //发送短信
                    $msg_send = new MsgService();
                    $azdId = $v['azd_id'];
                    $jxs = RlZsmsUserQueryModel::Where('bm_code', $v['jxs'])->where('gw', 0)->where('status', 1)->select()->toArray();
                    if (empty($jxs)) {
                        $datas['info'] = '经销商手机号不正确';
                        $datas['update_time'] = date("Y-m-d H:i:s", time());
                        $datas['status'] = 0;
                        Db::name('message_log')->where('azd_id', $azdId)->update($datas);
                    }else{
                        foreach ($jxs as $key => $value) {
                            $phone = $value['mobilphone'];
                            $msgres = $msg_send->installsend7($azdId,$phone);
                            if($msgres)
                            {
                                $dataone['update_time'] = date("Y-m-d H:i:s", time());
                                $dataone['status'] = 1;
                                $dataone['info'] = '发送成功';
                                Db::name('message_log')->where('azd_id', $azdId)->update($dataone);
                            }else{
                                $datatwo['update_time'] = date("Y-m-d H:i:s", time());
                                $datatwo['status'] = 1;
                                $datatwo['info'] = '发送失败';
                                Db::name('message_log')->where('azd_id', $azdId)->update($datatwo);
                            }
                        }
                    }
                }
            }
        }
    }
    public function getInfo(){
        $list=Db::name('message_log')->select()->toArray();
        echo "<table width='100%' border='0'><tr><td>id</td><td>安装单id</td><td>类型</td><td>创建时间</td><td>更新时间</td><td>状态</td><td>备注</td></tr>";
        foreach($list as $v){
            echo "<tr>";
            echo "<td>".$v['id']."</td>";
            echo "<td>".$v['azd_id']."</td>";
            echo "<td>".$v['type']."</td>";
            echo "<td>".$v['create_time']."</td>";
            echo "<td>".$v['update_time']."</td>";
            echo "<td>".$v['status']."</td>";
            echo "<td>".$v['info']."</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    public function Automaticbeifen(){
        $azd_limit_id = 0;
        $i=0;
        while (1)
        {
            $azd_arr = [];
            $sms = [];
            $sms2 = [];
            $azd_list = RlHiseServiceProjectModel::where('gczt','in','3,4')->where('ycgcsfjsbs',1)->limit($i*200,200)->select()->toArray();
           if(empty($azd_list))
            {
                break;
            }
            foreach ($azd_list as $v){
                $azd_limit_id = $v['hise_service_project_id'];
                $azd_id= $v['azd_id'];
                $yqsqzq= RlApplyDelayModel::where(['status' => 1,'hise_service_project_id' => $azd_limit_id,'azd_id' =>$azd_id])->sum('yqsqzq');
                $nine=strtotime("-9 month");//9个月之前的时间戳
                if($nine >=$v['ycgcwcsj']){
                    if($v['is_apply_delay']){
                        if(strtotime('-' . $yqsqzq . ' month',$nine)>=$v['ycgcwcsj']){
                            $azd_arr[] = $v['azd_id'];
                            $sms[] = $v;
                        }
                        continue;
                    }else{
                        $azd_arr[] = $v['azd_id'];
                        $sms[] = $v;
                    }
                }
            }
            if(config('installsend8')&&!empty($sms))
            {
                // 引用发送验证码类：安装单自动提交通知,短信接收人：经销商
                $msg_send = new MsgService();
                // 发送手机验证码
                foreach ($sms as $k => $v) {
                    $azdId = $v['azd_id'];
                    $jxs = RlZsmsUserQueryModel::Where('bm_code',$v['jxs'])->where('gw',0)->where('status',1)->select()->toArray();
                    foreach ($jxs as $key => $value) {
                        $phone = $value['mobilphone'];
                       // $msgres = $msg_send->installsend8($azdId,$phone);
                       // if($msgres)
                       // {
                         //   trace($phone.'||'.date('Y-m-d h:i:s',time()).'安装单自动提交通知短信发送成功','info');
                      //  }else{
                        //    trace($phone.'||'.date('Y-m-d h:i:s',time()).'安装单自动提交通知短信发送失败','info');
                        //}
                    }
                }
                unset($sms);
            }
            foreach ($azd_list as $v){
                $eight=strtotime("-8 month");//9个月之前的时间戳
                $azd_limit_id = $v['hise_service_project_id'];
                $azd_id= $v['azd_id'];
                $yqsqzq= RlApplyDelayModel::where(['status' => 1,'hise_service_project_id' => $azd_limit_id,'azd_id' =>$azd_id])->sum('yqsqzq');
                if(!$v['yqsqzq'])
                {
                    $yqsqzq = 0;
                }
               $yq=strtotime('-' . $yqsqzq . ' month',$eight);
                $endtime=$yq-172799;
                if($v['is_apply_delay']){
                    if($yq>=$v['ycgcwcsj']  and $yq-172799<=$v['ycgcwcsj'] ){
                        $sms2[] = $v;
                    }
                }elseif($eight >=$v['ycgcwcsj'] and $endtime<=$v['ycgcwcsj'] ){
                      $sms2[] = $v;
                   }

            }
            if(config('installsend7')&&!empty($sms2))
            {
                // 引用发送验证码类：安装单自动提交提醒,短信接收人：经销商
                $msg_send = new MsgService();
                // 发送手机验证码
                foreach ($sms2 as $k => $v) {
                    $azdId = $v['azd_id'];
                    $jxs = RlZsmsUserQueryModel::Where('bm_code',$v['jxs'])->where('gw',0)->where('status',1)->select()->toArray();
                    foreach ($jxs as $key => $value) {
                        $phone = $value['mobilphone'];
                        $msgres = $msg_send->installsend7($azdId,$phone);
                        if($msgres)
                        {
                            trace($phone.'||'.date('Y-m-d h:i:s',time()).'安装单自动提交提醒短信发送成功','info');
                        }else{
                            trace($phone.'||'.date('Y-m-d h:i:s',time()).'安装单自动提交提醒短信发送失败','info');
                        }
                    }

                }
            unset($sms2);
            }
            if(!empty($azd_arr)){
                $data = [
                    'zdyxbs'=>1,
                ];
                $data['gczt'] = 5;
                $data['update_time'] = time();
                $data['bx_kssj'] = time();
	            $data['tssj'] = time();
                $data['wgsj'] = time();
	             $data['tjrq'] = time();
                $azd_id = implode(',',$azd_arr);
                $row = RlHiseServiceProjectModel::Where('azd_id','in',$azd_id)->update($data);
                if($row){
                    Log::write('自动转运行成功'.date('Y-h-d h:i',time()));
                }else{
                    Log::write('自动转运行失败'.date('Y-h-d h:i',time()));
                }
            }else{
                Log::write('暂时没有自动转运行的安装单'.date('Y-h-d h:i',time()));
            }

            $i++;

        }



    }

}

